package com.example.jyapplication.ui;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.jyapplication.R;
import com.example.jyapplication.bean.FoodListBean;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import org.json.JSONObject;

import okhttp3.OkHttpClient;

public class FoodActivity extends AppCompatActivity implements View.OnClickListener {
    Context context;
    private static final String TAG = "detail";
    TextView tvFoodName,tvFoodPrice,tvFoodDescribe,tvFoodCount;
    ImageView ivFoodBack,ivDetailFood;
    Button btnBuyFood;
    ImageButton ibtnAdd,ibtnRemove;
    int count=0;
    Float price;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food);
        initView();
        getData();
    }

    private void getData() {
        // 从FoodListActivity中得到传过来的id数据
        Intent intent = getIntent();
        int id = intent.getIntExtra("id",0);
        // 利用得到的id数据进行网络访问得到数据
        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        String url = "http://192.168.43.221:8080/18/Menu/byId?id="+id;
        JSONObject jsonObject = new JSONObject();
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        Gson gson = new Gson();
                        FoodListBean.ListBean listBean = gson.fromJson(jsonObject.toString(),FoodListBean.ListBean.class);

                        // 将数据绑定到对应的TextView上
                        tvFoodName.setText("菜品名称："+listBean.getName());
                        tvFoodPrice.setText("菜品价格："+listBean.getPrice()+"元");
                        tvFoodDescribe.setText("菜品简介："+listBean.getDescription());
                        price = Float.parseFloat(listBean.getPrice());
                        Picasso.with(getApplicationContext())
                                .load("http://192.168.43.221:8080/18/statics/"+listBean.getIcon())
                                .into(ivDetailFood);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Toast.makeText(FoodActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
                    }
                });
        requestQueue.add(jsonObjectRequest);
    }

    private void initView() {
        // 绑定控件
        tvFoodDescribe = findViewById(R.id.tv_detail_describe);
        tvFoodName = findViewById(R.id.tv_detail_name);
        tvFoodPrice = findViewById(R.id.tv_detail_price);
        tvFoodCount = findViewById(R.id.tv_detail_count);

        ivDetailFood = findViewById(R.id.iv_detail_food);
        ivFoodBack = findViewById(R.id.iv_food_back);
        ivFoodBack.setOnClickListener(this);

        btnBuyFood = findViewById(R.id.btn_detail_buy);
        btnBuyFood.setOnClickListener(this);
        ibtnAdd = findViewById(R.id.btn_detail_add);
        ibtnAdd.setOnClickListener(this);
        ibtnRemove = findViewById(R.id.btn_detail_remove);
        ibtnRemove.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
    // 设置
        switch (v.getId()){
            // 点击订购按钮跳转
            case R.id.btn_detail_buy:
                if (count>0){
                    Toast.makeText(this, "订购成功", Toast.LENGTH_SHORT).show();
                    Intent intent1 = new Intent(this,FoodListActivity.class);
                    intent1.putExtra("num",count);
                    startActivity(intent1);
                }else {
                    Toast.makeText(this, "请选择订购数量", Toast.LENGTH_SHORT).show();
                }
                break;
            // 点击加号图片跳转
            case R.id.btn_detail_add:
                Log.e(TAG, "onClick: "+count );
                count+=1;
                tvFoodCount.setText("数量："+count);

                btnBuyFood.setText(price*count+"元    立即支付");
                break;
            // 点击减号图片跳转
            case R.id.btn_detail_remove:
                if (count>0){
                    count-=1;
                    tvFoodCount.setText("数量："+count);
                    btnBuyFood.setText(price*count+"元    立即支付");
                }else {
                    count=0;
                    btnBuyFood.setText(price*count+"0元    立即支付");
                }
                break;
            // 点击返回图片跳转
            case R.id.iv_food_back:
                Intent intent2 = new Intent(this,FoodListActivity.class);
                startActivity(intent2);
                break;
        }
    }
}